package org.example.mall.admin.configuration.auth.check;

import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import org.example.mall.admin.configuration.auth.code.ScenesConst;
import org.example.mall.admin.constant.auth.AuthTypeConst;
import org.example.mall.admin.constant.CommonConstants;
import org.example.mall.admin.util.SecurityUtil;
import org.example.mall.admin.configuration.SpringContextHolder;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hzq
 * @date 2022/1/3 11:27
 */
@Component
public class ImageCheckAuthHandler implements CheckAuthHandler {

    @Override
    public Object check(String scenes, String principal, String credentials) {
        HttpServletRequest request = SecurityUtil.getHttpServletRequest();
        CaptchaVO vo = new CaptchaVO();
        vo.setPointJson(request.getParameter("pointJson"));
        vo.setToken(request.getParameter("token"));
        vo.setCaptchaType(CommonConstants.IMAGE_CODE_TYPE);
        CaptchaService captchaService = SpringContextHolder.getBean(CaptchaService.class);
        return captchaService.check(vo);
    }

    @Override
    public boolean supportType(String checkType) {
        return AuthTypeConst.IMAGE.equals(checkType);
    }

    @Override
    public boolean supportScenes(String scenes) {
        return ScenesConst.LOGIN.equals(scenes);
    }
}
